package com.gz.springboot.config;

import com.gz.springboot.bean.Car;
import com.gz.springboot.bean.Dog;
import com.gz.springboot.bean.Furn;
import com.gz.springboot.bean.Monster;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import java.util.Date;

/**
 * @Configuration 表示是一个配置类，等价于配置文件
 * proxyBeanMethods = true 表示返回的组件是单例的 (默认)
 * proxyBeanMethods = false 表示返回的组件是新创建的，即多例的
 * proxyBeanMethods 是要先获取BeanConfig才能生效
 * 即BeanConfig beanConfig=ioc.getBean(BeanConfig.class)
 * beanConfig.mon
 */
//@EnableConfigurationProperties(Furn.class)
@Configuration(proxyBeanMethods = true)

//类上的条件注解，表示该类的所有方法都进行条件约束
//@ConditionalOnBean(name = "monster_new")
public class BeanConfig {

    /**
     * @Bean :给容器添加组件，相当于beans.xml中配置Monster bean对象
     *  这里的方法名相当于beans.xml中配置bean对象的id
     *  name或value 可以给bean对象指定名字/id,如果没有默认方法名
     */
//    @Bean(name = "monster_new")
//    @Bean(value = "monster_new")
    @Bean
//    @Scope("prototype") //多例
    public Monster monster01() {
        return new Monster(200,"牛魔王",500,"野蛮冲撞",new Date(),new Car("小白马",888.88));
    }

    @Bean
    /**
     *                  条件注解(方法上的)
     *  @ConditionalOnBean(name = "monster_new") 表示
     *  当容器中有一个Bean,名字是monster_new,就注入dog01这个Dog Bean,否则不注入
     *
     *  @ConditionalOnMissingBean(name = "monster_new") 表示
     *  与@ConditionalOnBean相反，如果ioc有name=monster_new的bean则不注入
     */
//    @ConditionalOnBean(name = "monster_new")
//    @ConditionalOnMissingBean(name = "monster_new")
    public Dog dog01() {
        return new Dog("小白");
    }
}
